<template>
  <div>
    <van-nav-bar
      title="订单列表"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <div v-if="list.length">
      <van-card
      @click="$router.push(`/orderlistdetail?id=${item.id}`)"
        v-for="item in list"
        :key="item.id"
        :num="item.content.length"
        :price="item.content | price"
        :desc="item.create_time | date"
        :title="item.contact.name"
        :thumb="item.content[0].images[0]"
      />
    </div>
    <div v-else>
      <van-empty
      description="您还没买任何东西"
      image="https://img2.baidu.com/it/u=3214428190,3968304704&fm=26&fmt=auto&gp=0.jpg"
      />
    >
    </div>
    <goup />
  </div>
</template>
<script>
import { getOrderList } from '@api/home'
import Goup from '@components/GoTop'
export default {
  name: 'orderlist',
  data () {
    return {
      list: []
    }
  },
  created () {
    this.init()
  },
  filters: {
    date (val) {
      var d = new Date(val)
      var m = d.getMinutes()
      var s = d.getSeconds()
      m = checkTime(m)
      s = checkTime(s)

      function checkTime (i) {
        if (i < 10) {
          i = '0' + i
        }
        return i
      }
      return (
        d.getFullYear() +
        '年' +
        (d.getMonth() + 1) +
        '月' +
        d.getDate() +
        '日' +
        d.getHours() +
        '时' +
        m +
        '分' +
        s + '秒'
      )
    },
    price (val) {
      return val.reduce((p, n) => {
        return p + n.num * n.price
      }, 0)
    }
  },
  methods: {
    onClickLeft () {
      this.$router.go(-1)
    },
    init () {
      getOrderList().then((res) => {
        if (res.data.code === 200) {
          this.list = res.data.data
        }
      })
    }
  },
  components: { Goup }
}
</script>
<style lang="scss" scoped>
</style>
